package fr.afcepf.ai89.esharing.data;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.bind.ParseConversionEvent;

import fr.afcepf.ai89.esharing.entity.FrCodePostalAsso;
import fr.afcepf.ai89.esharing.entity.FrCodePostalAssoPK;
import fr.afcepf.ai89.esharing.entity.FrCommune;
import fr.afcepf.ai89.esharing.entity.FrDepartement;
import fr.afcepf.ai89.esharing.entity.FrRegion;

@Stateless
public class DAOFrCodePostalAsso implements IDAOFrCodePostalAsso {

	@PersistenceContext(unitName = "ESharingPhase22")
	EntityManager em;

	@Override
	public FrCodePostalAsso getCpAsso(FrCodePostalAssoPK idCodePostal) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FrCodePostalAsso> getCpAsso() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FrCodePostalAsso> getCpAsso(FrRegion region) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FrCodePostalAsso> getCpAsso(FrDepartement dept) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FrCodePostalAsso> getCpAsso(FrCommune commune) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Integer> getCodesPostaux() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Integer> getCodesPostaux(FrRegion region) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Integer> getCodesPostaux(FrDepartement dept) {

		Query hql = em.createQuery("SELECT DISTINCT cpa.id.codePostal from FrCodePostalAsso cpa WHERE cpa.id.idDeptCom = :param");

		hql.setParameter("param", dept.getIdDepartement());
		
		List<Long> cps = (List<Long>)hql.getResultList();
		
		List<Integer> cpsI = new ArrayList<Integer>();
		
		for (Long cp : cps) {
			
			Integer cpI = cp.intValue();
			cpsI.add(cpI);
			
		}
		
//		for (FrCodePostalAsso cpa : cpas) {
//			
//			cps.add((int)cpa.getId().getCodePostal());
//			
//		}

		return cpsI;

	}

	@Override
	public List<Integer> getCodesPostaux(FrCommune commune) {

		Query hql = em.createQuery("SELECT DISTINCT cpa.id.codePostal from FrCodePostalAsso cpa WHERE cpa.id.idDeptCom = :param1 AND cpa.id.codeCommune = :param2");

		hql.setParameter("param1", commune.getId().getIdDepartement());
		hql.setParameter("param2", commune.getId().getCodeCommune());
		List<FrCodePostalAsso> cpas = (List<FrCodePostalAsso>)hql.getResultList();
		
		List<Integer> cps = new ArrayList<Integer>();
		
		for (FrCodePostalAsso cpa : cpas) {
			
			cps.add((int)cpa.getId().getCodePostal());
			
		}

		return cps;

	}

	@Override
	public int addCpAsso(FrCodePostalAsso cpAsso) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int majCpAsso(FrCodePostalAsso cpAsso) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<FrCommune> getCommunes(int codePostal) {
		
		Query hql = em.createQuery("SELECT DISTINCT frCommune from FrCodePostalAsso cpa WHERE cpa.id.codePostal = :param");

		long cp = (long)codePostal;
		hql.setParameter("param", cp);
		int i = 1;
		//System.out.println(i);
		List<FrCommune> communes = (List<FrCommune>)hql.getResultList();

		return communes;

	}

}
